﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            BlockingCollection<int> ints = new BlockingCollection<int>();

            //生产者
            Task producer = Task.Run(() =>
            {
                for (int i = 0; i < 10; i++)
                {
                    ints.Add(i);
                    Console.WriteLine("生产了：" + i);
                    Thread.Sleep(100);
                }
                ints.CompleteAdding();
            });

            //消费者
            Task consumer = Task.Run(() =>
            {
                foreach (int i in ints.GetConsumingEnumerable())
                {
                    Console.WriteLine("消费了" + i);
                    Thread.Sleep(100);
                }
            });
            Task.WaitAll(producer, consumer);
            Console.ReadKey();
        }
    }
}
